using System;
using System.Text.RegularExpressions;

namespace Neo.Framework
{
	/// <summary>
	/// Base class for all EntityObject surrogates. A surrogate is a lightweight representation of an entity disconnected from the data source.
	/// </summary>
	[Serializable]
	public abstract class EntityObjectSurrogate
	{
		private Regex _reg;

        /// <summary>
        /// Initializes a new instance of the <see cref="EntityObjectSurrogate"/> class.
        /// </summary>
		public EntityObjectSurrogate()
		{
			this._reg = new System.Text.RegularExpressions.Regex("</?(?i:script|body|embed|object|frameset|frame|iframe|meta|link|style)(.|\\n)*?>");
		}
		
		protected Regex StringValidation
		{
			get
			{
				return _reg;
			}
			set
			{
				_reg = value;
			}
		}
		
		protected virtual bool IsNull(object val)
        {
            if ((val == null))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        protected virtual bool IsInt16(object val)
        {
            try
            {
                System.Convert.ToInt16(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        protected virtual bool IsInt32(object val)
        {
            try
            {
                System.Convert.ToInt32(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        protected virtual bool IsInt64(object val)
        {
            try
            {
                System.Convert.ToInt64(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        protected virtual bool IsDouble(object val)
        {
            try
            {
                System.Convert.ToDouble(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        protected virtual bool IsDecimal(object val)
        {
            try
            {
                System.Convert.ToDecimal(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        protected virtual bool IsString(object val)
        {
            if (this._reg.IsMatch(val.ToString()))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        
        protected virtual bool IsDateTime(object val)
        {
            try
            {
                System.DateTime.Parse(val.ToString());
                return true;
            }
            catch 
            {
                return false;
            }
        }
        
        protected virtual bool IsByteArray(object val)
        {
            return true;
        }
        
        protected virtual bool IsGuid(object val)
        {
            try
            {
                System.Guid myGuid;
                myGuid = new System.Guid(val.ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
